// hyb_menzhen/detail/pages/order/order.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    array: ["微信支付"],
    address: '',
    argeen: false,
    xieyi: false,
    type: "",
    did: "",
    server: "",
    money: "",
    hid: "",
    keshi_two: "",
    tid: "",
    leixing: "",
    name: "",
    zid: "",
    time: "",
    week: "",
    start: "",
    end: "",
    j_id: "",
  },
  // 选择就诊人
  bindjiuzhenren(e) {
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi?type=' + this.data.leixing,
    })
  },
  // 选择支付方式
  bindPickerChange(e) {
    console.log(e)
    this.setData({
      index: e.detail.value
    })
  },
  // 使用优惠券
  bindyouhuiquan(e) {
    wx.navigateTo({
      url: '/hyb_menzhen/detail/pages/healthy_use/healthy_use',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var server = options.server;
    var money = options.money
    var hid = options.hid;

    var zid = options.zid;
    var time = options.time;
    var week = options.week;
    var start = options.start;
    var end = options.end;
    this.setData({

      server: server,
      money: money,
      hid: hid,
      leixing: 'lvtong',
      zid: zid,
      time: time,
      week: week,
      start: start,
      end: end,
      bgc: wx.getStorageSync('color'),
      title: "远程挂号"
    })
    that.getGreen();
    that.getxieyi();
  },
  getxieyi: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/green.get_Xieyi",
      data: {
        key_words: that.data.server
      },
      success: function (res) {
        that.setData({
          content: res.data.serh_xiey
        })
      }
    })
  },
  // 获取绿通
  getGreen: function (res) {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/zhuanjia.zhuanjia_detail",
      data: {
        zid: that.data.zid,
      },
      success: function (res) {
        that.setData({
          info: res.data
        })
      }
    })
  },
  // 是否同意协议
  radioChange() {
    this.setData({
      argeen: !this.data.argeen,
    })
    if (this.data.argeen) {
      this.setData({
        xieyi: true,
      })
    }
  },
  closezhe(e) {
    this.setData({
      xieyi: false,
    })
  },
  // 立即支付
  bindsubmit(e) {
    var that = this
    var tkid = app.globalData.tkid //一级
    var mytkid = app.globalData.mytkid //二级
    var ziji = app.globalData.ziji //查询当前是不是自己是一级分销商
    var j_id = that.data.j_id
    var value = e.detail.value
    if (j_id == "") {
      wx.showToast({
        title: '请选择就诊人',
        icon: 'none'
      })
    } else if (that.data.argeen == false) {
      wx.showToast({
        title: '请同意协议',
        icon: 'none'
      })
    } else {
      app.util.request({
        'url': "entry/wxapp/zhuanjia.add_ghorder",
        data: {
          openid: wx.getStorageSync('openid'),
          did: that.data.did,
          server: that.data.server,
          money: that.data.money,
          hid: that.data.hid,
          time: that.data.time,
          j_id: that.data.j_id,
          zid: that.data.zid,
          week: that.data.week,
          time: that.data.time,
          start_time: that.data.start,
          end_time: that.data.end,
        },
        success: function (res) {
          var info = res.data;
          if (that.data.money == '' || that.data.money == '0' || that.data.money == '0.00') {
            app.util.request({
              'url': 'entry/wxapp/yuyue.mobelghmsgdh',
              data: {
                orders: info.back_orser,
                keywords: '挂号'
              },
              success: function (res) {
                console.log(res)
              }
            })


            //短信提醒
            app.util.request({
              'url': 'entry/wxapp/yuyue.guhaomsgdh',
              data: {
                orders: info.back_orser,
                text: '挂号预约'
              },
              success: function (res) {
                console.log(res)

              }
            })
            wx.showToast({
              title: '预约成功！',
              icon: 'none',
              success: function () {
                setTimeout(function () {
                  wx.reLaunch({
                    url: '/hyb_yl/zhuanjiasubpages/pages/postoperative_ordercont/postoperative_ordercont?txt=yes&back_orser=' + info.back_orser + '&key_words=' + info.key_words + '&j_id=' + that.data.j_id + '&zid=' + that.data.zid + '&ifpay=1' + '&money=' + that.data.money + '&id=' + info.id,
                  })
                }, 1900)
              }
            })
          } else {
            app.util.request({
              'url': 'entry/wxapp/green.payorder',
              header: {
                "Content-Type": "application/xml"
              },
              method: "GET",
              data: {
                openid: wx.getStorageSync('openid'),
                z_tw_money: info.money,
                orders: info.orders,
                key_words: that.data.server
              },
              success: function (res) {
                console.log(res);
                wx.requestPayment({
                  'timeStamp': res.data.timeStamp,
                  'nonceStr': res.data.nonceStr, //字符串随机数
                  'package': res.data.package,
                  'signType': res.data.signType,
                  'paySign': res.data.paySign,
                  success: function (res) {
                    console.log(res)

                  },
                  fail: function (res) {
                    console.log(res)

                  },
                  'complete': function (res) {
                    if (res.errMsg == 'requestPayment:ok') {
                      app.util.request({
                        'url': "entry/wxapp/zhuanjia.updateghorder",
                        data: {
                          back_orser: info.orders,
                        }
                      })
                      //产生分销提成
                      if (ziji == 0) {
                        app.util.request({
                          'url': 'entry/wxapp/tuike.uptopmoney',
                          data: {
                            tkid: tkid,
                            mytkid: mytkid,
                            money: info.money,
                            openid: wx.getStorageSync('openid'),
                            leixing: 'yuanchengguahao'
                          },
                          success: function (res) {
                            console.log(res)
                          }
                        })
                      }
                      app.util.request({
                        'url': 'entry/wxapp/yuyue.mobelghmsgdh',
                        data: {
                          orders: info.back_orser,
                          keywords: '挂号'
                        },
                        success: function (res) {
                          console.log(res)
                        }
                      })
          
          
                      //短信提醒
                      app.util.request({
                        'url': 'entry/wxapp/yuyue.guhaomsgdh',
                        data: {
                          orders: info.back_orser,
                          text: '挂号预约'
                        },
                        success: function (res) {
                          console.log(res)
          
                        }
                      })
                      wx.showToast({
                        title: '预约成功！',
                        icon: 'none',
                        success: function () {
                          setTimeout(function () {
                            wx.reLaunch({

                              url: '/hyb_yl/zhuanjiasubpages/pages/postoperative_ordercont/postoperative_ordercont?txt=yes&back_orser=' + info.back_orser + '&key_words=' + info.key_words + '&j_id=' + that.data.j_id + '&zid=' + that.data.zid + '&ifpay=1' + '&money=' + that.data.money + '&id=' + info.id,
                            })
                          }, 1900)
                        }
                      })
                      return;
                    }
                  }
                })
              }
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      address: wx.getStorageSync('address')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})